/**
 * 报表上报公共方法
 */
import { httpAction, getAction, postAction } from '@/api/manage'
import moment from 'moment'
import 'moment/locale/zh-cn'
export const YzzxListMixin = {
  data() {
    return {}
  },
  mounted() { },
  computed: {},
  methods: {
    // 获取单位字符串
    updateQueryParam(data) {
      debugger
      console.log('data------>', data)
      let str1 = ''
      let str2 = ''
      let str3 = ''
      data.map(item => {
        str1 += item.accCode + ','
        str2 += item.accName + ','
        str3 += item.unitId + ','
      })
      if (str1.length > 0) {
        str1 = str1.substr(0, str1.length - 1)
      }
      if (str2.length > 0) {
        str2 = str2.substr(0, str2.length - 1)
      }
      if (str3.length > 0) {
        str3 = str3.substr(0, str3.length - 1)
      }
      this.queryParam.accCode = str1
      this.queryParam.accName = str2
      this.queryParam.unitId = str3
    },
    // 多选科目编码
    onSelectSubject(data) {
      let str1 = ''
      let str2 = ''
      data.map(item => {
        str1 += item.subjectCode + ','
        str2 += item.subjectName + ','
      })
      if (str1.length > 0) {
        str1 = str1.substr(0, str1.length - 1)
      }
      if (str2.length > 0) {
        str2 = str2.substr(0, str2.length - 1)
      }
      this.queryParam.subjectCode = str1
      this.queryParam.subjectName = str2
    },
    // 凭证查询下拉
    getDocTree() {
      getAction(this.url.docTree).then(res => {
        this.loading = false
        if (res.success) {
          this.docTreeData = res.result
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    // 科目编码下拉树
    getSubjectTree(id) {
      this.subjectTreeData = []
      // getAction(this.url.subjectTree, { pcode: '0' }).then(res => {
      //   this.loading = false
      //   if (res.success) {
      //     res.result.map((item) => {
      //       this.subjectTreeData.push({
      //         id: item.subjectCode,
      //         pId: item.pcode,
      //         value: item.subjectCode,
      //         title: item.subjectName,
      //         isLeaf: item.isLowest == 1 ? true : false
      //       })
      //     })
      //   } else {
      //     this.$message.warning(res.message)
      //   }
      // })
      getAction(this.url.subjectTree).then(res => {
        this.loading = false
        if (res.success) {
          this.subjectTreeData = this.getTreeData(res.result)
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    getTreeData(data) {
      const arr = []
      let children = []
      data.map(item => {
        if (item.children.length) {
          children = this.getTreeData(item.children)
          arr.push(Object.assign({}, item, { isLeaf: item.isLeft == 1, children }))
        } else {
          arr.push(Object.assign({}, item, { isLeaf: item.isLeft == 1 }))
        }
      })
      return arr
    },
    // 科目编码下拉树结构
    onLoadData(treeNode) {
      const _this = this
      const childTree = []
      return new Promise(resolve => {
        if (treeNode.dataRef.children) {
          // 有值了直接渲染
          resolve()
          return
        }
        // 没有值根据当前父节点获取下面子节点并挂在树节点中，添加到对应父节点的children中
        getAction(this.url.subjectTree, { pcode: treeNode.dataRef.id }).then(res => {
          this.loading = false
          if (res.success) {
            res.result.map(item => {
              childTree.push({
                id: item.subjectCode,
                pId: item.pcode,
                value: item.subjectCode,
                title: item.subjectName,
                isLeaf: item.isLowest == 1
              })
            })
            this.subjectTreeData = this.subjectTreeData.concat(childTree)
          } else {
            this.$message.warning(res.message)
          }
        })
        resolve()
      })
    },
    moment,
    // 得到年份选择器的值
    handlePanelChange(dateArray) {
      if (dateArray.length === 0) {
        this.queryParam.startTime = this.queryParam.endTime = ''
        return
      }
      this.queryParam.startTime = moment(dateArray[0]).format('YYYY-MM-DD')
      this.queryParam.endTime = moment(dateArray[1]).format('YYYY-MM-DD')
    }
  }
}
